#pragma once
#include "windows.h"

namespace MyType
{
	template<typename T>
	class Value
	{
		BYTE m_abyData[sizeof(T)];
	public:
		Value()
		{
			ZeroMemory(m_abyData,sizeof(m_abyData));
		}

		Value(const T& rkData)
		{
			*this = rkData;
		}

		Value& operator=(const T& rkData)
		{
			int iSize = sizeof(T);
			for (int i = 0;i < sizeof(T);i++)
			{
				m_abyData[i] = ((rkData >> ((--iSize) * 8))) & (0xFF);
			}

			return *this;
		}

		operator T()const
		{
			T kValue = 0;
			int iSize = sizeof(T);
			for (int i = 0;i < sizeof(T);i++)
			{
				kValue |= (((T)m_abyData[i]) << ((--iSize) * 8));
			}

			return kValue;
		}
	};

	typedef bool			_bool;
	typedef char			_char;
	typedef BYTE			_BYTE;
	typedef Value<short>	_short;
	typedef Value<WORD>		_WORD;
	typedef Value<int>		_int;
	typedef Value<UINT>		_UINT;
	typedef Value<int>		_BOOL;
	typedef Value<int>		_ENUM;
	typedef Value<INT64>	_INT64;
	typedef Value<UINT64>	_UINT64;
	typedef float			_float;
	typedef double			_double;
}

using namespace MyType;

#define MAX_MSG_LEN (0xFFFF)
#define MAX_DATA_LEN (MAX_MSG_LEN * 2)

struct MyDataPack
{
	char acData[MAX_DATA_LEN];
	DWORD dwDataLen;

	MyDataPack()
	{
		ZeroMemory(this,sizeof(*this));
	}
};

enum ParsePackError
{
	PPE_INVALID_PARAM,
	PPE_CHECK_HEAD_FAILED,
	PPE_WAIT_FOR_DATA,
	PPE_NO_ERROR,
};

#define MYEXPORT __declspec(dllexport)
#define MYAPI __stdcall

typedef void (MYAPI *pfOnNetMsg)(const char* pcData,int iLen,const void* pkParam);

MYEXPORT BYTE MYAPI CalCheck(const char* pcData,int iLen);
MYEXPORT BOOL MYAPI CheckHead(const char* pcData,int iLen);

MYEXPORT BOOL MYAPI PackMsg(MyDataPack& rkPack,const char* pcData,int iLen,BOOL bEncode = FALSE,BOOL bZip = FALSE);
MYEXPORT ParsePackError MYAPI ParsePack(MyDataPack& rkPack,const char* pcBuf,int iLen);

MYEXPORT int MYAPI Encode(const char* pcSrc,int iSrcLen,char* pcDst,int iDstLen);
MYEXPORT int MYAPI Decode(const char* pcSrc,int iSrcLen,char* pcDst,int iDstLen);

MYEXPORT int MYAPI Zip(const char* pcSrc,int iSrcLen,char* pcDst,int iDstLen);
MYEXPORT int MYAPI UnZip(const char* pcSrc,int iSrcLen,char* pcDst,int iDstLen);

MYEXPORT void MYAPI ProcNetIOBuf(const char* pcBuf,int& riLen,pfOnNetMsg pfNetMsgProc,const void* pkParam);